package com.yypx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yypx.common.utils.ObjToMapUtil;
import com.yypx.dao.StudentDAO;
import com.yypx.entity.Student;
import com.yypx.entity.StudentType;
import com.yypx.entity.TypeValueObject;
import com.yypx.service.CertificateTypeService;
import com.yypx.service.StudentService;
import com.yypx.service.StudentTypeService;

@Component("studentService")
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentDAO studentDAO;
	@Autowired
	private CertificateTypeService certificateTypeService;
	@Autowired
	private StudentTypeService studentTypeService;

	@Override
	public void add(Student o) {
		studentDAO.addNew(o);
	}

	@Override
	public List<Student> selAll() {
		return studentDAO.selAll();
	}

	@Override
	public List<Map<String, Object>> getResultList(List<Student> list) {
		if (list == null)
			return null;
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			HashMap<String, Object> resultItem = (HashMap<String, Object>) ObjToMapUtil.toHashMap(list.get(i));
			/* 在这个地方可以根据具体的情况对实体的属性的页面返回做处理，比如说1代表可用 ，0代表不可用 */
			handleEntityForPageTemplate(resultItem, list.get(i));
			result.add(resultItem);
		}
		return result;
	}

	@Override
	public void handleEntityForPageTemplate(Map<String, Object> map, Student student) {
		if (student.getGender() != null) {
			if (student.getGender() == 0) {
				map.put("gender", "女");
			} else {
				map.put("gender", "男");
			}
			map.put("gendervalue", student.getGender());
		}
		if (student.getCertificate() != null) {
			TypeValueObject t = certificateTypeService.findById(student.getCertificate());
			map.put("certificate", t.getTname());
			map.put("certvale", student.getCertificate());
		}
		if (student.getStatus() != null) {
			StudentType s = studentTypeService.findById(student.getStatus());
			map.put("status", s.getTname());
			map.put("statusvalue", student.getStatus());
		}
	}

	@Override
	public List<Student> selAllByCondition(Map map) {
		return studentDAO.selAllByCondition(map);
	}

	@Override
	public int selCountByCondition(Map map) {
		return studentDAO.selCountByCondition(map);
	}

	@Override
	public Student findById(UUID id) {
		return studentDAO.findById(id);
	}

	@Override
	public Map<String, Object> getResultMap(Student o) {
		HashMap<String, Object> resultItem = (HashMap<String, Object>) ObjToMapUtil.toHashMap(o);
		/* 在这个地方可以根据具体的情况对实体的属性的页面返回做处理，比如说1代表可用 ，0代表不可用 */
		handleEntityForPageTemplate(resultItem, o);
		return resultItem;
	}

	@Override
	public void update(Student o) {
		studentDAO.update(o);
	}

	@Override
	public void delById(UUID id) {
		studentDAO.delById(id);
	}
}
